package com.guli.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.Constant;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;
import com.guli.product.dao.AttrAttrgroupRelationDao;
import com.guli.product.dao.AttrDao;
import com.guli.product.dao.AttrGroupDao;
import com.guli.product.entity.AttrAttrgroupRelationEntity;
import com.guli.product.entity.AttrEntity;
import com.guli.product.entity.AttrGroupEntity;
import com.guli.product.service.AttrAttrgroupRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrDao attrDao;
    @Autowired
    private AttrGroupDao attrGroupDao;


    @Override
    public PageUtils queryPage(Long attrGroupId, Map<String, Object> params) {
        //根据属性分组Id查询该行数据
        AttrGroupEntity attrGroup = attrGroupDao.selectOne(new QueryWrapper<AttrGroupEntity>().eq("attr_group_id", attrGroupId));
        //获取分类Id
        Long categoryId = attrGroup.getCategoryId();
        //根据属性分组Id查询未关联的所有属性
        List<AttrAttrgroupRelationEntity> attrgroupRelations = attrAttrgroupRelationDao
                .selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().isNull("attr_group_id"));
        //查询这些属性中为该分类Id的属性
        List<Long> attrIds = attrgroupRelations.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        QueryWrapper<AttrEntity> wrapper;
        IPage<AttrEntity> pages = new Page<>();
        if (!CollectionUtils.isEmpty(attrIds)) {
            //查询该分类下的所有基本属性  1代表基本属性
            wrapper = new QueryWrapper<AttrEntity>().eq("category_id", categoryId).eq("attr_type", 1).in("attr_id", attrIds);
            String key = (String) params.get(Constant.KEY);
            //判断是否具有关键字搜索
            if (!StringUtils.isEmpty(key)) {
                wrapper.like("attr_name", key).or().eq("attr_id", key);
            }
            pages = attrDao.selectPage(new Query<AttrEntity>().getPage(params), wrapper);
        }
        return new PageUtils(pages);
    }

    @Override
    public List<AttrEntity> attrGroupRelation(Long attrGroupId) {
        //根据分组Id查询分类Id
        AttrGroupEntity attrGroup = attrGroupDao.selectOne(new QueryWrapper<AttrGroupEntity>()
                .eq("attr_group_id", attrGroupId));
        Long catelogId = attrGroup.getCategoryId();
        //根据attGroupId查询attrId
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelations = attrAttrgroupRelationDao
                .selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
        List<AttrEntity> attrList = null;
        if (!CollectionUtils.isEmpty(attrAttrgroupRelations)) {
            //获取与该属性分组相关联的属性Id
            List<Long> attrIds = attrAttrgroupRelations.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(attrIds)) {
                //根据attrIds查询所有属性
                attrList = attrDao.selectList(new QueryWrapper<AttrEntity>()
                        .eq("category_id", catelogId).eq("attr_type", 1).in("attr_id", attrIds));
            }
        }
        return attrList;
    }

    @Override
    public void updateAttr(Long id) {
        attrAttrgroupRelationDao.updateAttr(id);
    }
}